package com.hd.crdao.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hd.crdao.R;
import com.hd.crdao.activity.DelayPayMoneyActivity;
import com.hd.crdao.activity.GpsLocationDetailsActivity;
import com.hd.crdao.activity.LoanCarInfoActivity;
import com.hd.crdao.activity.RepaymentDetailActivity;
import com.hd.crdao.activity.TodayCarCheckActivity;
import com.hd.crdao.bean.LoanCarListBean;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.StringUtils;
import com.hd.crdao.utils.UIUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Programming
 * @date 2017/10/17 0017
 */

public class NewLoanCarRVAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<LoanCarListBean.RowsBean> mDatas;
    private LoanCarListBean mLoanCarListBean;


    public NewLoanCarRVAdapter(Context context, List<LoanCarListBean.RowsBean> mDatas, LoanCarListBean loanCarListBean) {
        mContext = context;
        this.mLoanCarListBean = loanCarListBean;
        this.mDatas = mDatas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.loancar_header, parent, false);
            return new HeaderHolder(view);
        } else {
            View view = LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.newloancar_item, parent, false);
            return new ViewHolder(view);
        }


    }


    @SuppressLint("RecyclerView")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder viewHolder;
        HeaderHolder headerHolder;
        if (getItemViewType(position) == 0) {
            headerHolder = (HeaderHolder) holder;
            headerHolder.mCreditAmount.setText(StringUtils.doubleToString(mLoanCarListBean.getCreditAmount()));
            headerHolder.mInterestAmount.setText(StringUtils.doubleToString(mLoanCarListBean.getInterestAmount()));
        } else {
            viewHolder = (ViewHolder) holder;
            ALog.d("" + position);
            final LoanCarListBean.RowsBean rowsBean = mDatas.get(position - 1);
            if (rowsBean.getCheckStatus() == 0) {          //盘点的状态
                viewHolder.mItemLoancarHead.setImageResource(R.drawable.pan_2x);
            } else if (rowsBean.getCheckStatus() == 1) {
                viewHolder.mItemLoancarHead.setImageResource(R.drawable.yipand_2x);
            }

            viewHolder.mVeBrand.setText(rowsBean.getVeBrand());
            viewHolder.mVeFrame.setText(rowsBean.getVeFrame());
            viewHolder.mInterestMoney.setText(StringUtils.doubleToString(rowsBean.getInterestMoney()));
            viewHolder.mInterestDays.setText(rowsBean.getInterestDays());
            viewHolder.mVcRate.setText(String.valueOf(rowsBean.getVehCredit().getVcRate()) + "%");
            viewHolder.mVcCredit.setText(StringUtils.doubleToString(rowsBean.getVehCredit().getVcCredit()));
            if (rowsBean.getProgressDate() != null) {
                viewHolder.mProgressDate.setText(rowsBean.getVehCredit().getFkDate());
            } else {
                viewHolder.mProgressDate.setText("");
            }
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {       //条目点击事件
                    Intent intent = new Intent(UIUtils.getContext(), LoanCarInfoActivity.class);
                    intent.putExtra("carId", rowsBean.getId());      //传入一个车辆id 查询车辆的详细情况
                    intent.putExtra("vehCreditId", rowsBean.getVehCredit().getId());
                    intent.putExtra("veBrand", rowsBean.getVeBrand());
                    intent.putExtra("veFrame", rowsBean.getVeFrame());
                    mContext.startActivity(intent);


                }
            });
            viewHolder.mGpsLocation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(UIUtils.getContext(), GpsLocationDetailsActivity.class);
                    intent.putExtra("vehicleTblId", rowsBean.getId());//此处传值可能有错
                    mContext.startActivity(intent);
                }
            });

            viewHolder.mCheckCar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {       //盘点车辆
                    Intent intent = new Intent(UIUtils.getContext(), TodayCarCheckActivity.class);
                    intent.putExtra("vehicleId", rowsBean.getId());
                    mContext.startActivity(intent);
                }
            });
            viewHolder.mDelayPayMoney.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {       //申请展期
                    Intent intent = new Intent(UIUtils.getContext(), DelayPayMoneyActivity.class);
                    intent.putExtra("state", rowsBean.getVehCredit().getExtendFlag());
                    ALog.d("state:" + rowsBean.getVehCredit().getExtendFlag());
                    intent.putExtra("vehCreditId", rowsBean.getVehCredit().getId());
                    ALog.d(rowsBean.getId());
                    intent.putExtra("veBrand", rowsBean.getVeBrand() + "(" + rowsBean.getTranPlate() + ")");
                    intent.putExtra("vcCredit", rowsBean.getVehCredit().getVcCredit() + "");
                    intent.putExtra("vcRate", rowsBean.getVehCredit().getVcRate() + "%");
                    intent.putExtra("fkDate", rowsBean.getVehCredit().getFkDate());

                    mContext.startActivity(intent);
                }
            });
            viewHolder.mRepaymentDetail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {       //还款详情
                    Intent intent = new Intent(UIUtils.getContext(), RepaymentDetailActivity.class);
                    intent.putExtra("vehCreditId", rowsBean.getVehCredit().getId());
                    ALog.d(rowsBean.getId());
                    mContext.startActivity(intent);
                }
            });

        }


    }

    @Override
    public int getItemCount() {
        if (mDatas != null) {
            return mDatas.size() + 1;
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        }
        return 1;
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_loancar_head)
        ImageView mItemLoancarHead;
        @BindView(R.id.veBrand)
        TextView mVeBrand;
        @BindView(R.id.interestDays)
        TextView mInterestDays;
        @BindView(R.id.interestMoney)
        TextView mInterestMoney;
        @BindView(R.id.vcRate)
        TextView mVcRate;
        @BindView(R.id.veFrame)
        TextView mVeFrame;
        @BindView(R.id.vcCredit)
        TextView mVcCredit;
        @BindView(R.id.gpsLocation)
        Button mGpsLocation;
        @BindView(R.id.checkCar)
        Button mCheckCar;
        @BindView(R.id.delayPayMoney)
        Button mDelayPayMoney;
        @BindView(R.id.repaymentDetail)
        Button mRepaymentDetail;
        @BindView(R.id.progressDate)
        TextView mProgressDate;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    class HeaderHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.creditAmount)
        TextView mCreditAmount;
        @BindView(R.id.interestAmount)
        TextView mInterestAmount;

        HeaderHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
